<?php

/**
 * This is the model class for table "{{book_comment}}".
 *
 * The followings are the available columns in table '{{book_comment}}':
 * @property string $id
 * @property integer $pid
 * @property integer $bid
 * @property integer $bcid
 * @property string $content
 * @property integer $parent
 * @property integer $create_time
 * @property integer $good
 * @property integer $role
 * @property string $image
 * @property string $user_name
 * @property integer $start
 * @property integer $sex
 */
class BookComment extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{book_comment}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('pid, bid, bcid, parent, create_time, good, role, start, sex', 'numerical', 'integerOnly'=>true),
			array('image', 'length', 'max'=>255),
			array('user_name', 'length', 'max'=>20),
			array('content', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, pid, bid, bcid, content, parent, create_time, good, role, image, user_name, start, sex', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'pid' => 'Pid',
			'bid' => 'Bid',
			'bcid' => 'Bcid',
			'content' => 'Content',
			'parent' => 'Parent',
			'create_time' => 'Create Time',
			'good' => 'Good',
			'role' => 'Role',
			'image' => 'Image',
			'user_name' => 'User Name',
			'start' => 'Start',
			'sex' => 'Sex',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('pid',$this->pid);
		$criteria->compare('bid',$this->bid);
		$criteria->compare('bcid',$this->bcid);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('parent',$this->parent);
		$criteria->compare('create_time',$this->create_time);
		$criteria->compare('good',$this->good);
		$criteria->compare('role',$this->role);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('user_name',$this->user_name,true);
		$criteria->compare('start',$this->start);
		$criteria->compare('sex',$this->sex);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return BookComment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
    public function getInfo($first,$total,$bid){
        $db = Yii::app()->db->createCommand();
        $db->select('*');
        $db->from('{{book_comment}}');
        $db->andWhere('bid=:bid and parent=:parent',
            array(':bid'=>$bid,':parent'=>0));
        $db->order('create_time desc');
        $db->limit($total,$first);
        $info = $db->queryAll();
        return $info;
    }
    public function getComment($pid){
        $db = Yii::app()->db->createCommand();
        $info = $db
            ->select('bc.*,u.is_relation')
            ->from('{{book_comment}} bc')
            ->leftJoin('{{people}} u','bc.pid=u.id')
            ->where('bc.parent=:parent',array(':parent'=>$pid))
            ->order('bc.create_time desc')
            ->queryAll();
        return $info;
    }
    public function getShare($pid){
        $db = Yii::app()->db->createCommand();
        $info = $db
            ->select('bc.*,b.thumb,b.title')
            ->from('{{book_comment}} bc')
            ->leftJoin('{{book}} b','bc.bid=b.id')
            ->where('bc.pid=:pid and bc.parent=:parent',array(':pid'=>$pid,':parent'=>0))
            ->order('bc.create_time desc')
            ->limit(100)
            ->queryAll();
        return $info;
    }
    public function getStudentComment($cid,$first,$total,$title='',$key=1,$uid){
        if($key==1){
            if($title){
                $sqltitle = " AND t4.user_name LIKE '%$title%'";
            }else{
                $sqltitle = "";
            }
            $orderSql =" ORDER BY t4.create_time DESC";
            $limitSql =" LIMIT $first,$total";
            $db = Yii::app()->db->createCommand("SELECT * FROM (SELECT * FROM (SELECT bc.*,b.thumb,b.title FROM {{book_comment}} bc LEFT JOIN {{book}} b ON bc.bid=b.id WHERE b.cid=$cid and bc.parent=0 and role=1) t1 LEFT JOIN (SELECT bcc.parent as ppt FROM {{book_comment}} bcc LEFT JOIN {{book}} bb ON bcc.bid=bb.id  WHERE bcc.pid=$uid) t3 ON t3.ppt=t1.id) t4 WHERE ISNULL(t4.ppt) $sqltitle $orderSql $limitSql");
        }else{
            if($title){
                $sqltitle = " AND t4.user_name LIKE '%$title%'";
            }else{
                $sqltitle = "";
            }
            $orderSql =" ORDER BY t4.create_time DESC";
            $limitSql =" LIMIT $first,$total";
            $db = Yii::app()->db->createCommand("SELECT * FROM (SELECT * FROM (SELECT bc.*,b.thumb,b.title FROM {{book_comment}} bc LEFT JOIN {{book}} b ON bc.bid=b.id WHERE b.cid=$cid and bc.parent=0 and role=1) t1 LEFT JOIN (SELECT bcc.parent as ppt FROM {{book_comment}} bcc LEFT JOIN {{book}} bb ON bcc.bid=bb.id  WHERE bcc.pid=$uid) t3 ON t3.ppt=t1.id) t4 WHERE t4.ppt !='' $sqltitle $orderSql $limitSql");
        }
        $info = $db->queryAll();
        return $info;
    }
    public function getChildComment($child_id,$first,$total){
        $db = Yii::app()->db->createCommand();
        $db->select('bc.*,b.thumb,b.title');
        $db->from('{{book_comment}} bc');
        $db->leftJoin('{{book}} b','bc.bid=b.id');
        $db->andWhere('bc.pid=:pid and bc.parent=:parent',array(':pid'=>$child_id,':parent'=>0));
        $db->order('bc.create_time desc');
        $db->limit($total,$first);
        $info = $db->queryAll();
        return $info;
    }
}
